@using Elsa.Studio.Models
@using Elsa.Api.Client.Resources.Scripting.Models
@using Elsa.Api.Client.Shared.UIHints.SingleLine
@using Elsa.Studio.UIHints.Extensions
@using MudBlazor.Utilities

@{
    var inputDescriptor = EditorContext.InputDescriptor;
    var displayName = inputDescriptor.DisplayName;
    var description = inputDescriptor.Description;
    var inputValue = EditorContext.GetLiteralValueOrDefault();
    var adornment = !string.IsNullOrWhiteSpace(_singleLineProps.AdornmentText) ? Adornment.Start : Adornment.None;
}

<ExpressionInput EditorContext="@EditorContext">
    <ChildContent>
        <MudTextField
            T="string"
            Label="@displayName"
            Variant="Variant.Outlined"
            HelperText="@description"
            Margin="Margin.Dense"
            ReadOnly="EditorContext.IsReadOnly"
            Disabled="EditorContext.IsReadOnly"
            Adornment="@adornment"
            AdornmentText="@_singleLineProps.AdornmentText"
            AdornmentColor="Color.Info"
            Value="@inputValue"
            ValueChanged="OnValueChanged"/>
    </ChildContent>
</ExpressionInput>

@code {

    private SingleLineProps _singleLineProps = new();

    /// Gets or sets the editor context.
    [Parameter]
    public DisplayInputEditorContext EditorContext { get; set; } = default!;

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        _singleLineProps = EditorContext.InputDescriptor.GetSingleLineProps();
    }

    private async Task OnValueChanged(string newValue)
    {
        var expression = Expression.CreateLiteral(newValue.TrimWhitespace());

        await EditorContext.UpdateExpressionAsync(expression);
    }

}